package com.huatai.bi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.bi.bo.DataSetColumnBO;
import com.huatai.bi.entity.DataProcessEntity;
import com.huatai.bi.entity.DataSetColumnEntity;
import com.huatai.bi.vo.DataProcessVO;
import com.huatai.bi.vo.DirctProcessVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author hecai
* @description 针对表【tb_data_process(数据集加工描述)】的数据库操作Mapper
* @createDate 2024-06-27 18:33:47
* @Entity com.huatai.bi.DataProcess
*/
public interface DataProcessMapper extends BaseMapper<DataProcessEntity> {

	/**
	 * 获取指标数据项
	 * @param dtId
	 * @return
	 */
	@Select(" select b.*,a.full_sql " +
		"from tb_data_set_column as b  " +
		"left join tb_data_process as a on a.id=b.data_set_id " +
		"where a.type='aggregate' and a.dt_id='${dtId}' ")
	List<DataSetColumnBO> getDataSetColumnByAggregate(@Param("dtId")Long dtId);


	@Select(" SELECT " +
            " a.id, " +
            " a.`name`, " +
            " a.full_sql, " +
            " a.create_by, " +
            " a.create_time, " +
            " a.description, " +
            " ( SELECT GROUP_CONCAT( b.`name` ) FROM tb_data_process_info AS b WHERE a.id = b.dp_id limit 1 ) AS typeName  " +
            "FROM " +
            " tb_data_process AS a " +
            "where a.is_deleted = 0 AND a.dt_id=${id} ")
    IPage<DataProcessVO> getPageData(IPage<DataProcessVO> page,@Param("id")Long id);

	@Select("<script>" +
		" select * " +
		" from (select a.dt_id,a.name,a.create_by,a.create_time, " +
		" (SELECT GROUP_CONCAT(c.name,',') FROM tb_data_process AS c WHERE a.dt_id = c.dt_id and c.is_deleted=0 ) as processName, " +
		" ( SELECT count( * ) FROM tb_data_process AS b WHERE a.dt_id = b.dt_id and b.is_deleted=0 ) AS totalSize " +
		" from tb_directory as a " +
		" where a.is_dir=1 and a.is_deleted=0 ) T " +
		" where  1=1  " +
		"<if test='dataSetName!=null and dataSetName!=\"\" '>" +
		" AND a.name LIKE '%${dataSetName}%'  " +
		"</if>" +
		" <if test='processName!=null and processName!=\"\" '>" +
		" AND processName LIKE '%${processName}%' "+
		"</if>" +
		"</script>")
	IPage<DirctProcessVO> getDirctProcessPageData(IPage<DirctProcessVO> page,
												  @Param("dataSetName")String dataSetName,
												  @Param("processName")String processName);

	@Select("select * from tb_data_process where id = ${id}")
	DataProcessEntity get(@Param("id")Long id);

	@Select("select id from tb_data_process where is_deleted = 0 and dt_id = ${dtId}")
	List<Long> listIdByDtId(@Param("dtId")Long dtId);
}




